package com.huaxin.hxcloud.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.BaseFragment;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.HxCheckOrgBean;
import com.huaxin.common.Global;
import com.huaxin.common.frendcircle.utils.CommonUtils;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.RequestHelper;
import com.huaxin.common.tts.Constant;
import com.huaxin.common.utils.HxAppClearMgr;
import com.huaxin.common.utils.HxUtils;
import com.huaxin.common.utils.PrefUtil;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.common.view.avatarimageview.AvatarImageView;
import com.huaxin.common.view.avatarimageview.HxAvatarImageGlide;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.activity.AboutActivity;
import com.huaxin.hxcloud.activity.FanSwitchActivity;
import com.huaxin.hxcloud.activity.FanUserInfoActivity;
import com.huaxin.hxcloud.activity.FeedBackActivity;
import com.huaxin.hxcloud.activity.ModifyUserInfoActivity;
import com.huaxin.hxcloud.activity.SettingNotificationActivity;
import com.huaxin.hxcloud.main.HomeActivity;
import com.huaxin.hxcloud.main.LoginActivity;
import com.huaxindata.im.main.manager.UserManager;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 我的Fragment
 * Created by Administrator on 2016/9/7.
 */
public class MyPagerFragment extends BaseFragment {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.rl_notification_settings)
    RelativeLayout rlNotificationSettings;

    //意见反馈
    @BindView(R.id.setting_feedback)
    TextView       settingFeedback;
    @BindView(R.id.rl_feedback)
    RelativeLayout rlFeedback;
    @BindView(R.id.rl_check_updata)
    RelativeLayout rl_check_updata;

    @BindView(R.id.rl_about)
    RelativeLayout rlAbout;

    @BindView(R.id.pager_icon_round_iv)
    AvatarImageView imageView;

    @BindView(R.id.pager_nice_name_txt)
    TextView niceName;
    @BindView(R.id.pager_my_mode_txt)
    TextView mode;
    @BindView(R.id.my_pager_com_tv)
    TextView mPagerComTv;

    private ProfileFragmentCallback mProfileFragmentCallback;
    private EditText                mEdit;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_pager_news2;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ProfileFragmentCallback)
            this.mProfileFragmentCallback = (ProfileFragmentCallback) context;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        if (Constant.SCOrgcodeFirst.equals(UserManager.getFirstOrgCode())) {
            rl_check_updata.setVisibility(View.VISIBLE);
        } else {
            rl_check_updata.setVisibility(View.GONE);
        }

    }

    protected void updateView() {
        //设置图片等
        if (Global.user != null) {
            HxAvatarImageGlide instance = HxAvatarImageGlide.getInstance();
            instance.setHeadImage(getContext(), Global.user.getImgUrl(), Global.user.getNickName(), imageView);
            //loadImg(imageView, Global.user.getImgUrl());
            loadText(niceName, Global.user.getNickName());
            if (TextUtils.isEmpty(Global.user.getCheckedState() + "")) {
                loadText(mode, "游客模式");
            } else {
                loadText(mode, "正式用户");
            }
            setAmendState(Global.getUser().getCheckedState());
        }
    }


    @Override
    protected void onResumeView() {
        super.onResumeView();
        ((BaseActivity) getActivity()).setSupportActionBar(toolbar);
        ((BaseActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //                startActivity(new Intent(getActivity(), ScanCaptureActivity.class));
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        updateView();
    }


    @OnClick({R.id.rl_notification_settings, R.id.rl_notification_collection,
            R.id.rl_notification_circle, R.id.rl_notification_fanswitch,
            R.id.rl_regular_orgcode, R.id.rl_about, R.id.my_rl_exit1,
            R.id.rl_feedback, R.id.pager_more, R.id.rl_check_updata})
    public void OnClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.rl_notification_settings:
                intent = new Intent(getContext(), SettingNotificationActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_feedback: //意见反馈
                intent = new Intent(getContext(), FeedBackActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_about:
                intent = new Intent(getContext(), AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.pager_more: //我的资料
                startActivity(new Intent(getActivity(), ModifyUserInfoActivity.class));
                break;
            case R.id.my_rl_exit1:
                mProfileFragmentCallback.onExit();
                //如果设置了登出，则可能是想切换帐号,将自动登录取消
                PrefUtil.set(Constants.PK_AUTO_LOGIN, false);
                PrefUtil.set(Constants.PK_SAVE_PW, false);
                intent = new Intent(getContext(), LoginActivity.class);
                //清理缓存
                HxAppClearMgr.cleanApplicationData(getContext());
                HxTransmitRequestHelper.getBackLogin();
                //极光将别名设置未null 避免登出的情况下还能收到推送消息
                // JPushInterface.setAlias(getActivity(),0,null);
                JPushInterface.deleteAlias(getActivity(), 21027);
                startActivity(intent);
                getActivity().finish();
                break;
            case R.id.rl_regular_orgcode://申请企业账户
                //0 = 未审核
                //1 = 审核中
                //2 = 已审核
                switch (Global.user.getCheckedState()) {
                    case 0:
                        regularOrgCode();
                        break;
                }

                break;
            case R.id.rl_notification_fanswitch://我的关注
                intent = new Intent(getContext(), FanSwitchActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_notification_circle://我的圈子
                intent = new Intent(getContext(), FanUserInfoActivity.class);
                intent.putExtra("userId", 5);
                startActivity(intent);
                break;
            case R.id.rl_notification_collection://我的收藏
                intent = new Intent(getContext(), FanUserInfoActivity.class);
                intent.putExtra("userId", 3);
                startActivity(intent);
                break;
            case R.id.rl_check_updata: //版本更新
                HomeActivity activity = (HomeActivity) getActivity();
                activity.upDateVersion(HxUtils.getVersionName(getActivity()), 0);
                break;
            default:
                break;
        }
    }

    //申请企业账户
    private void regularOrgCode() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_regular_orgcode, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        final AlertDialog alertDialog = builder.setView(view).create();
        alertDialog.show();
        TextView mSure = (TextView) view.findViewById(R.id.fragment_orgcode_s);
        TextView mNag = (TextView) view.findViewById(R.id.fragment_orgcode_n);
        mEdit = (EditText) view.findViewById(R.id.fragment_orgcode_e);
        mSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {  //提交审核
                submitOrgCode(mEdit.getText().toString());
                alertDialog.dismiss();
                CommonUtils.hideSoftInput(getContext(), mEdit);
            }
        });
        mNag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.hideSoftInput(getContext(), mEdit);
                alertDialog.dismiss();
            }
        });
    }

    /**
     * @desc 提交企业认证
     * @author songxf
     * created at 2017/8/5 14:56
     */
    private void submitOrgCode(final String orgCode) {
        Call<HxCheckOrgBean> call = RequestHelper.getRequestApi().checkOrgCode(orgCode);
        call.enqueue(new Callback<HxCheckOrgBean>() {
            @Override
            public void onResponse(Call<HxCheckOrgBean> call, Response<HxCheckOrgBean> response) {
                HxCheckOrgBean orgBean = response.body();
                if (response.isSuccessful() && orgBean != null) {
                    ToastUtil.showToast(orgBean.getMsg());
                    setAmendState(orgBean.getCheckedState());
                    Global.user.setCheckedState(orgBean.getCheckedState());
                }
            }

            @Override
            public void onFailure(Call<HxCheckOrgBean> call, Throwable t) {
                ToastUtil.showToastShort("网络故障");
            }
        });

    }

    /**
     * @desc 修改认证状态
     * @author songxf
     * created at 2017/8/9 9:59
     */
    private void setAmendState(int checkedState) {
        //0 = 未审核
        //1 = 审核中
        //2 = 已审核
        switch (checkedState) {
            case 0:
                loadText(mPagerComTv, "未审核");
                break;
            case 1:
                loadText(mPagerComTv, "审核中");
                break;
            case 2:
                loadText(mPagerComTv, "正式企业用户");
                break;
        }

    }

    public interface ProfileFragmentCallback {
        //切换帐号
        void onExit();
    }
}
